home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / libini.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-15  |  13.8 KB  |  499 lines  |  [TEXT/MMCC]

  1. /*
  2.  * libini.c - initialization and teardown routines.
  3.  *
  4.  * Author:  Eric Haines, 3D/Eye, Inc.
  5.  *
  6.  */
  7.  
  8. /*-----------------------------------------------------------------*/
  9. /* include section */
  10. /*-----------------------------------------------------------------*/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <string.h>
  16.  
  17. #include "lib.h"
  18. #include "drv.h"
  19.  
  20.  
  21. /*-----------------------------------------------------------------*/
  22. /* defines/constants section */
  23. /*-----------------------------------------------------------------*/
  24.  
  25.  
  26.  
  27. /*-----------------------------------------------------------------*/
  28. /* Library initialization/teardown functions */
  29. /*-----------------------------------------------------------------*/
  30. int
  31. lib_open( raytracer_format, filename )
  32. int     raytracer_format ;
  33. char    *filename ;     /* unused except for Mac version */
  34. {
  35. #ifdef OUTPUT_TO_FILE
  36.     /* no stdout, so write to a file! */
  37.     if (raytracer_format == OUTPUT_VIDEO) {
  38.     gStdout_file = stdout;
  39.     } else {
  40.     gStdout_file = fopen(filename,"w");
  41.     if ( gStdout_file == NULL ) return 1 ;
  42.     }
  43. #endif /* OUTPUT_TO_FILE */
  44.  
  45.     lib_set_output_file(gStdout_file);
  46.  
  47.     gRT_orig_format = raytracer_format;
  48.     if ((raytracer_format == OUTPUT_RTRACE) ||
  49.     (raytracer_format == OUTPUT_PLG))
  50.     lib_set_raytracer(OUTPUT_DELAYED);
  51.     else if (raytracer_format == OUTPUT_RWX) {
  52.        fprintf(gOutfile, "ModelBegin\n");
  53.        fprintf(gOutfile, "ClumpBegin\n");
  54.        fprintf(gOutfile, "LightSampling Vertex\n");
  55.        lib_set_raytracer(raytracer_format);
  56.        }
  57.     else
  58.     lib_set_raytracer(raytracer_format);
  59.  
  60.  
  61.     return 0 ;
  62. }
  63.  
  64. /*-----------------------------------------------------------------*/
  65. void
  66. lib_close PARAMS((void))
  67. {
  68.     /* Make sure everything is cleaned up */
  69.     if ((gRT_orig_format == OUTPUT_RTRACE) ||
  70.     (gRT_orig_format == OUTPUT_PLG)) {
  71.     lib_set_raytracer(gRT_orig_format);
  72.     lib_flush_definitions();
  73.     }
  74.  
  75.     if (gRT_out_format == OUTPUT_RIB) {
  76.     fprintf(gOutfile, "WorldEnd\n");
  77.     fprintf(gOutfile, "FrameEnd\n");
  78.     }
  79.     else if (gRT_out_format == OUTPUT_DXF) {
  80.     fprintf(gOutfile, "  0\n");
  81.     fprintf(gOutfile, "ENDSEC\n");
  82.     fprintf(gOutfile, "  0\n");
  83.     fprintf(gOutfile, "EOF\n");
  84.     }
  85.     else if (gRT_out_format == OUTPUT_RWX) {
  86.     fprintf(gOutfile, "ClumpEnd\n");
  87.     fprintf(gOutfile, "ModelEnd\n");
  88.     }
  89.  
  90. #ifdef OUTPUT_TO_FILE
  91.     /* no stdout, so close our output! */
  92.     if (gStdout_file)
  93.     fclose(gStdout_file);
  94. #endif /* OUTPUT_TO_FILE */
  95.     if (gRT_out_format == OUTPUT_VIDEO)
  96.     display_close(1);
  97. }
  98.  
  99.  
  100. /*-----------------------------------------------------------------*/
  101. void lib_storage_initialize PARAMS((void))
  102. {
  103.     gPoly_vbuffer = (unsigned int*)malloc(VBUFFER_SIZE * sizeof(unsigned int));
  104.     gPoly_end = (int*)malloc(POLYEND_SIZE * sizeof(int));
  105.     if (!gPoly_vbuffer || !gPoly_end) {
  106.     fprintf(stderr,
  107.         "Error(lib_storage_initialize): Can't allocate memory.\n");
  108.     exit(1);
  109.     }
  110. } /* lib_storage_initialize */
  111.  
  112.  
  113. /*-----------------------------------------------------------------*/
  114. void
  115. lib_storage_shutdown PARAMS((void))
  116. {
  117.     if (gPoly_vbuffer) {
  118.     free(gPoly_vbuffer);
  119.     gPoly_vbuffer = NULL;
  120.     }
  121.     if (gPoly_end) {
  122.     free(gPoly_end);
  123.     gPoly_end = NULL;
  124.     }
  125. } /* lib_storage_shutdown */
  126.  
  127.  
  128. /*-----------------------------------------------------------------*/
  129. void show_gen_usage PARAMS((void))
  130. {
  131.     /* localize the usage strings to be expanded only once for space savings */
  132. #if defined(applec) || defined(THINK_C)
  133.     /* and don't write to stdout on Macs, which don't have console I/O, and  */
  134.     /* won't ever get this error anyway, since parms are auto-generated.     */
  135. #else
  136.  
  137.     fprintf(stderr, "usage [-s size] [-r format] [-c|t]\n");
  138.     fprintf(stderr, "-s size - input size of database\n");
  139.     fprintf(stderr, "-r format - input database format to output:\n");
  140.     fprintf(stderr, "   0   Output direct to the screen (sys dependent)\n");
  141.     fprintf(stderr, "   1   NFF - MTV\n");
  142.     fprintf(stderr, "   2   POV-Ray 1.0\n");
  143.     fprintf(stderr, "   3   Polyray v1.4, v1.5\n");
  144.     fprintf(stderr, "   4   Vivid 2.0\n");
  145.     fprintf(stderr, "   5   QRT 1.5\n");
  146.     fprintf(stderr, "   6   Rayshade\n");
  147.     fprintf(stderr, "   7   POV-Ray 2.0 (format is subject to change)\n");
  148.     fprintf(stderr, "   8   RTrace 8.0.0\n");
  149.     fprintf(stderr, "   9   PLG format for use with rend386\n");
  150.     fprintf(stderr, "   10  Raw triangle output\n");
  151.     fprintf(stderr, "   11  art 2.3\n");
  152.     fprintf(stderr, "   12  RenderMan RIB format\n");
  153.     fprintf(stderr, "   13  Autodesk DXF format (polygons only)\n");
  154.     fprintf(stderr, "   14  Wavefront OBJ format (polygons only)\n");
  155.     fprintf(stderr, "   15  RenderWare RWX script file\n");
  156.     fprintf(stderr, "-c - output true curved descriptions\n");
  157.     fprintf(stderr, "-t - output tessellated triangle descriptions\n");
  158.  
  159. #endif
  160. } /* show_gen_usage */
  161.  
  162. void show_read_usage PARAMS((void))
  163. {
  164.     /* localize the usage strings to be expanded only once for space savings */
  165. #if defined(applec) || defined(THINK_C)
  166.     /* and don't write to stdout on Macs, which don't have console I/O, and  */
  167.     /* won't ever get this error anyway, since parms are auto-generated.     */
  168. #else
  169.  
  170.     fprintf(stderr, "usage [-f filename] [-r format] [-c|t]\n");
  171.     fprintf(stderr, "-f filename - file to import/convert/display\n");
  172.     fprintf(stderr, "-r format - format to output:\n");
  173.     fprintf(stderr, "   0   Output direct to the screen (sys dependent)\n");
  174.     fprintf(stderr, "   1   NFF - MTV\n");
  175.     fprintf(stderr, "   2   POV-Ray 1.0\n");
  176.     fprintf(stderr, "   3   Polyray v1.4, v1.5\n");
  177.     fprintf(stderr, "   4   Vivid 2.0\n");
  178.     fprintf(stderr, "   5   QRT 1.5\n");
  179.     fprintf(stderr, "   6   Rayshade\n");
  180.     fprintf(stderr, "   7   POV-Ray 2.0 (format is subject to change)\n");
  181.     fprintf(stderr, "   8   RTrace 8.0.0\n");
  182.     fprintf(stderr, "   9   PLG format for use with rend386\n");
  183.     fprintf(stderr, "   10  Raw triangle output\n");
  184.     fprintf(stderr, "   11  art 2.3\n");
  185.     fprintf(stderr, "   12  RenderMan RIB format\n");
  186.     fprintf(stderr, "   13  Autodesk DXF format (polygons only)\n");
  187.     fprintf(stderr, "   14  Wavefront OBJ format (polygons only)\n");
  188.     fprintf(stderr, "   15  RenderWare RWX script file\n");
  189.     fprintf(stderr, "-c - output true curved descriptions\n");
  190.     fprintf(stderr, "-t - output tessellated triangle descriptions\n");
  191.  
  192. #endif
  193. } /* show_read_usage */
  194.  
  195.  
  196. /*-----------------------------------------------------------------*/
  197. /*
  198.  * Command line option parser for db generator
  199.  *
  200.  * -s size - input size of database (1 to N)
  201.  * -r format - input database format to output
  202.  *    0  Output direct to the screen (sys dependent)
  203.  *    1  NFF - MTV
  204.  *    2  POV-Ray 1.0
  205.  *    3  Polyray v1.4, v1.5
  206.  *    4  Vivid 2.0
  207.  *    5  QRT 1.5
  208.  *    6  Rayshade
  209.  *    7  POV-Ray 2.0 (format is subject to change)
  210.  *    8  RTrace 8.0.0
  211.  *    9  PLG format for use with "rend386"
  212.  *   10  Raw triangle output
  213.  *   11  art 2.3
  214.  *   12  RenderMan RIB format
  215.  *   13  Autodesk DXF format
  216.  *   14  Wavefront OBJ format
  217.  *   15  RenderWare RWX format
  218.  * -c - output true curved descriptions
  219.  * -t - output tessellated triangle descriptions
  220.  *
  221.  * TRUE returned if bad command line detected
  222.  * some of these are useless for the various routines - we're being a bit
  223.  * lazy here...
  224.  */
  225. int     lib_gen_get_opts( argc, argv, p_size, p_rdr, p_curve )
  226. int     argc ;
  227. char    *argv[] ;
  228. int     *p_size, *p_rdr, *p_curve ;
  229. {
  230. int num_arg ;
  231. int val ;
  232.  
  233.     num_arg = 0 ;
  234.  
  235.     while ( ++num_arg < argc ) {
  236.     if ( (*argv[num_arg] == '-') || (*argv[num_arg] == '/') ) {
  237.         switch( argv[num_arg][1] ) {
  238.         case 'c':       /* true curve output */
  239.             *p_curve = OUTPUT_CURVES ;
  240.             break ;
  241.         case 't':       /* tessellated curve output */
  242.             *p_curve = OUTPUT_PATCHES ;
  243.             break ;
  244.         case 'r':       /* renderer selection */
  245.             if ( ++num_arg < argc ) {
  246.             sscanf( argv[num_arg], "%d", &val ) ;
  247.             if ( val < OUTPUT_VIDEO || val >= OUTPUT_DELAYED ) {
  248.                 fprintf( stderr,
  249.                     "bad renderer value %d given\n",val);
  250.                 show_gen_usage();
  251.                 return( TRUE ) ;
  252.             }
  253.             *p_rdr = val ;
  254.             } else {
  255.             fprintf( stderr, "not enough args for -r option\n" ) ;
  256.             show_gen_usage();
  257.             return( TRUE ) ;
  258.             }
  259.             break ;
  260.         case 's':       /* size selection */
  261.             if ( ++num_arg < argc ) {
  262.             sscanf( argv[num_arg], "%d", &val ) ;
  263.             if ( val < 1 ) {
  264.                 fprintf( stderr,
  265.                     "bad size value %d given\n",val);
  266.                 show_gen_usage();
  267.                 return( TRUE ) ;
  268.             }
  269.             *p_size = val ;
  270.             } else {
  271.             fprintf( stderr, "not enough args for -s option\n" ) ;
  272.             show_gen_usage();
  273.             return( TRUE ) ;
  274.             }
  275.             break ;
  276.         default:
  277.             fprintf( stderr, "unknown argument -%c\n",
  278.                 argv[num_arg][1] ) ;
  279.             show_gen_usage();
  280.             return( TRUE ) ;
  281.         }
  282.     } else {
  283.         fprintf( stderr, "unknown argument %s\n",
  284.             argv[num_arg] ) ;
  285.         show_gen_usage();
  286.         return( TRUE ) ;
  287.     }
  288.     }
  289.     return( FALSE ) ;
  290. }
  291. /*-----------------------------------------------------------------*/
  292. /*
  293.  * Command line option parser for db reader (converter/displayer)
  294.  *
  295.  * -f filename - file to import/convert/display
  296.  * -r format - input database format to output
  297.  *    0  Output direct to the screen (sys dependent)
  298.  *    1  NFF - MTV
  299.  *    2  POV-Ray 1.0
  300.  *    3  Polyray v1.4, v1.5
  301.  *    4  Vivid 2.0
  302.  *    5  QRT 1.5
  303.  *    6  Rayshade
  304.  *    7  POV-Ray 2.0 (format is subject to change)
  305.  *    8  RTrace 8.0.0
  306.  *    9  PLG format for use with "rend386"
  307.  *   10  Raw triangle output
  308.  *   11  art 2.3
  309.  *   12  RenderMan RIB format
  310.  *   13  Autodesk DXF format
  311.  *   14  Wavefront OBJ format
  312.  *   15  RenderWare RWX format
  313.  * -c - output true curved descriptions
  314.  * -t - output tessellated triangle descriptions
  315.  *
  316.  * TRUE returned if bad command line detected
  317.  * some of these are useless for the various routines - we're being a bit
  318.  * lazy here...
  319.  */
  320. int     lib_read_get_opts( argc, argv, p_rdr, p_curve, p_infname )
  321. int     argc ;
  322. char    *argv[] ;
  323. int     *p_rdr, *p_curve ;
  324. char *p_infname;
  325. {
  326. int num_arg ;
  327. int val ;
  328.  
  329.     num_arg = 0 ;
  330.  
  331.     while ( ++num_arg < argc ) {
  332.     if ( (*argv[num_arg] == '-') || (*argv[num_arg] == '/') ) {
  333.         switch( argv[num_arg][1] ) {
  334.         case 'c':       /* true curve output */
  335.             *p_curve = OUTPUT_CURVES ;
  336.             break ;
  337.         case 't':       /* tessellated curve output */
  338.             *p_curve = OUTPUT_PATCHES ;
  339.             break ;
  340.         case 'f':       /* input file name */
  341.             if ( p_infname == NULL ) {
  342.             fprintf( stderr, "-f option not allowed\n" ) ;
  343.             show_read_usage();
  344.             return( TRUE ) ;
  345.             } else {
  346.             if ( ++num_arg < argc ) {
  347.                 sscanf( argv[num_arg], "%s", p_infname ) ;
  348.             } else {
  349.                 fprintf( stderr, "not enough args for -f option\n" ) ;
  350.                 show_read_usage();
  351.                 return( TRUE ) ;
  352.             }
  353.             }
  354.             break ;
  355.         case 'r':       /* renderer selection */
  356.             if ( ++num_arg < argc ) {
  357.             sscanf( argv[num_arg], "%d", &val ) ;
  358.             if ( val < OUTPUT_VIDEO || val >= OUTPUT_DELAYED ) {
  359.                 fprintf( stderr,
  360.                     "bad renderer value %d given\n",val);
  361.                 show_read_usage();
  362.                 return( TRUE ) ;
  363.             }
  364.             *p_rdr = val ;
  365.             } else {
  366.             fprintf( stderr, "not enough args for -r option\n" ) ;
  367.             show_read_usage();
  368.             return( TRUE ) ;
  369.             }
  370.             break ;
  371.         default:
  372.             fprintf( stderr, "unknown argument -%c\n",
  373.                 argv[num_arg][1] ) ;
  374.             show_read_usage();
  375.             return( TRUE ) ;
  376.         }
  377.     } else {
  378.         fprintf( stderr, "unknown argument %s\n",
  379.             argv[num_arg] ) ;
  380.         show_read_usage();
  381.         return( TRUE ) ;
  382.     }
  383.     }
  384.     return( FALSE ) ;
  385. }
  386.  
  387.  
  388. /*-----------------------------------------------------------------*/
  389. void
  390. lib_clear_database()
  391. {
  392.     surface_ptr ts1, ts2;
  393.     object_ptr to1, to2;
  394.     light_ptr tl1, tl2;
  395.  
  396.     gOutfile = stdout;
  397.     gTexture_name = NULL;
  398.     gTexture_count = 0;
  399.     gObject_count = 0;
  400.     gTexture_ior = 1.0;
  401.     gRT_out_format = OUTPUT_RT_DEFAULT;
  402.     gU_resolution = OUTPUT_RESOLUTION;
  403.     gV_resolution = OUTPUT_RESOLUTION;
  404.     SET_COORD3(gBkgnd_color, 0.0, 0.0, 0.0);
  405.     SET_COORD3(gFgnd_color, 0.0, 0.0, 0.0);
  406.  
  407.     /* Remove all surfaces */
  408.     ts1 = gLib_surfaces;
  409.     while (ts1 != NULL) {
  410.     ts2 = ts1;
  411.     ts1 = ts1->next;
  412.     free(ts2->surf_name);
  413.     free(ts2);
  414.     }
  415.     gLib_surfaces = NULL;
  416.  
  417.     /* Remove all objects */
  418.     to1 = gLib_objects;
  419.     while (to1 != NULL) {
  420.     to2 = to1;
  421.     to1 = to1->next_object;
  422.     free(to2);
  423.     }
  424.     gLib_objects = NULL;
  425.  
  426.     /* Remove all lights */
  427.     tl1 = gLib_lights;
  428.     while (tl1 != NULL) {
  429.     tl2 = tl1;
  430.     tl1 = tl1->next;
  431.     free(tl2);
  432.     }
  433.     gLib_lights = NULL;
  434.  
  435.     /* Reset the view */
  436.  
  437.     /* Deallocate polygon buffer */
  438.     if (gPoly_vbuffer != NULL)
  439.     lib_storage_shutdown();
  440.  
  441.     /* Clear vertex counters for polygons */
  442.     gVertex_count = 0; /* Vertex coordinates */
  443.     gNormal_count = 0; /* Vertex normals */
  444.  
  445.     /* Clear out the polygon stack */
  446.     to1 = gPolygon_stack;
  447.     while (to1 != NULL) {
  448.     to2 = to1;
  449.     to1 = to1->next_object;
  450.     free(to2);
  451.     }
  452.     gPolygon_stack = NULL;
  453. }
  454.  
  455. /*-----------------------------------------------------------------*/
  456. void
  457. lib_flush_definitions()
  458. {
  459.     switch (gRT_out_format) {
  460.        case OUTPUT_RTRACE:
  461.        case OUTPUT_VIDEO:
  462.        case OUTPUT_NFF:
  463.        case OUTPUT_POVRAY_10:
  464.        case OUTPUT_POLYRAY:
  465.        case OUTPUT_VIVID:
  466.        case OUTPUT_QRT:
  467.        case OUTPUT_RAYSHADE:
  468.        case OUTPUT_POVRAY_20:
  469.        case OUTPUT_PLG:
  470.        case OUTPUT_OBJ:
  471.        case OUTPUT_RWX:
  472.        case OUTPUT_RAWTRI:
  473.         lib_output_viewpoint(gViewpoint.from, gViewpoint.at, gViewpoint.up, gViewpoint.angle,
  474.             gViewpoint.aspect, gViewpoint.hither, gViewpoint.resx, gViewpoint.resy);
  475.  
  476.         lib_output_background_color(gBkgnd_color);
  477.  
  478.         dump_all_lights();
  479.  
  480.         dump_reorder_surfaces();
  481.  
  482.         dump_all_surfaces();
  483.  
  484.         dump_all_objects();
  485.  
  486.         if (gRT_out_format == OUTPUT_RTRACE)
  487.         fprintf(gOutfile, "Textures\n\n");
  488.  
  489.         break;
  490.        case OUTPUT_DELAYED:
  491.         fprintf(stderr, "Error: Renderer not selected before flushing\n");
  492.         exit(1);
  493.     }
  494.  
  495.     if (gRT_out_format == OUTPUT_PLG)
  496.     /* An extra step is needed to build the polygon file. */
  497.     dump_plg_file();
  498. }
  499.